<?php
require_once('../../includes/classes/core.php');
$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);

		break;
	case 'get_edit_page':
		$black_id 	= $_REQUEST['id'];
		
		$page		= GetPage(Getblack_list($black_id));

		$data		= array('page'	=> $page);

		break;
	case 'get_list' :
		$count	= $_REQUEST['count'];
		$hidden	= $_REQUEST['hidden'];
	  
		$rResult = mysql_query("SELECT 	black_list.id,
										black_list.`date`,
										black_list.`phone`,
										crystal_users.`name`,
										black_list.`comment`
							    FROM 	black_list
								JOIN crystal_users ON black_list.user_id=crystal_users.id
							    WHERE 	black_list.actived=1");
								  
		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

		break;
	case 'save_black_list':
		$black_id 	= $_REQUEST['id'];
		$phone		= $_REQUEST['phone'];
		
		$comment 	= $_REQUEST['comment'];
		if($phone != ''){
			if(!Checkblack_listExist($phone, $black_id)){
				if ($black_id == '') {
					Addblack_list($phone, $comment);
				}else {
					saveblack_list($black_id, $phone);
				}				
			} else {
				$error = '"' . $phone . '" უკვე არის სიაში!';
			}
		}

		break;
	case 'disable':
		$black_id	= $_REQUEST['id'];
		Disableblack_list($black_id);
		$res = mysql_fetch_assoc(mysql_query("SELECT  black_list.phone 
												FROM `black_list`
												WHERE id=$black_id
			"));
		$remm_phone=$res['phone'];
		
		$_REQUEST['act']="black_list_remove";		
		require_once "../asterisk/asterisk.action.php";
		
		

		break;
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Category Functions
* ******************************
*/

function Addblack_list($phone, $comment)
{
	$c_date		= date('Y-m-d H:i:s');
	$user_id	= $_SESSION['USERID'];
	mysql_query("INSERT INTO 	`black_list`
								(`user_id`,`date`,`phone`,`comment`)
					VALUES 		('$user_id','$c_date','$phone','$comment')");

}

function Saveblack_list($black_id, $phone)
{
	
	$user_id	= $_SESSION['USERID'];
	mysql_query("UPDATE `black_list`
				 SET    `user_id`='$user_id',
				 		`phone` = '$phone'
				 WHERE	`id` = $black_id");
	
}

function Disableblack_list($black_id)
{

	mysql_query("	UPDATE `black_list`
					SET    `actived` = 0
					WHERE	`id` = $black_id");
	
}

function Checkblack_listExist($phone)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT `id`
											FROM   `black_list`
											WHERE  `name` = '$phone' && `actived` = 1"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}


function Getblack_list($black_id)
	{
	$res = mysql_fetch_assoc(mysql_query("SELECT 	`id`,
													`date`,
													`phone`,
													`comment`
											FROM   	`black_list`
											WHERE  	`id` = $black_id" ));

	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="CallType">თარიღი</label></td>
					<td>
						<input type="text" id="date" class="idle address" onblur="this.className=\'idle address\'" value="' . $res['date'] . '"disabled="disabled" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="CallType">ნომერი</label></td>
					<td>
						<input type="text" id="phone" class="idle address" onblur="this.className=\'idle address\'" onfocus="this.className=\'activeField address\'" value="' . $res['phone'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="CallType">კომენტარი</label></td>
					<td>	
						<textarea  style="width: 225px; resize: none;" id="comment" class="idle" name="call_content" cols="300" rows="4">' . $res['comment'] . '</textarea>
					</td>
				</tr>
				
			</table>
			<!-- ID -->
			<input type="hidden" id="black_list_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>
